#
# Copyright 2017, Data61
# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
# ABN 41 687 119 230.
#
# This software may be distributed and modified according to the terms of
# the BSD 2-Clause license. Note that NO WARRANTY is provided.
# See "LICENSE_BSD2.txt" for details.
#
# @TAG(DATA61_BSD)
#

cmake_minimum_required(VERSION 3.5.1)

project(libmuslc NONE)

set(LIB_MUSLC_PARALLEL_BUILD true CACHE BOOL "Use `make -j` when building libmuslc")

# Construct the muslc version of our arch
if(Kernel64)
    if(${KernelArch} STREQUAL "arm")
        set(muslc_arch "aarch64")
    endif()
    if(${KernelArch} STREQUAL "x86")
        set(muslc_arch "x86_64")
    endif()
    if(${KernelArch} STREQUAL "riscv")
        set(muslc_arch "riscv")
    endif()
else()
    if(${KernelArch} STREQUAL "arm")
        set(muslc_arch "arm")
    endif()
    if(${KernelArch} STREQUAL "x86")
        set(muslc_arch "i386")
    endif()
    if(${KernelArch} STREQUAL "riscv")
        set(muslc_arch "riscv")
    endif()
endif()

set(
    LIB_MUSLC_GLOB_SOURCES OFF
    CACHE BOOL "This flag causes CMake to add lots of source files to its dependency lists
     which slows down its configuration times.
     When not making changes in the musllibc directory, it is likely not necessary
     to have this enabled."
)
mark_as_advanced(LIB_MUSLC_PARALLEL_BUILD LIB_MUSLC_GLOB_SOURCES)
if(LIB_MUSLC_GLOB_SOURCES)
    file(
        GLOB_RECURSE
            deps
            **/*.c
            **/*.h
            **/*.S
            **/*.s
    )
endif()

set(parallel "")
if(LIB_MUSLC_PARALLEL_BUILD)
    include(ProcessorCount)
    ProcessorCount(N)
    if(NOT N EQUAL 0)
        # We set tasks to number of CPUs + 2 to account for blocking IO
        # this is a similar heuristic to what Ninja uses by default.
        # Note that this gets used in a rule generated by add_custom_command and
        # is additive with any existing Ninja jobs.
        math(EXPR Nplus2 "${N} + 2")
        set(parallel "-j${Nplus2}")
    else()
        message(WARNING "Could not detect ProcessorCount for building musllibc.")
    endif()
endif()

# We make an attempt to extract compiler options from CMake
get_property(compile_options DIRECTORY PROPERTY COMPILE_OPTIONS)

# Also inherit the CMAKE build type flags from one of the following variables depending on
# the configured build type:
# CMAKE_C_FLAGS_DEBUG, CMAKE_C_FLAGS_RELEASE, CMAKE_C_FLAGS_RELWITHDEBINFO, CMAKE_C_FLAGS_MINSIZEREL
string(TOUPPER CMAKE_BUILD_TYPE build_type)
set(cmake_built_type_config_name CMAKE_C_FLAGS_${build_type})
separate_arguments(cmake_c_flags_sep NATIVE_COMMAND "${CMAKE_C_FLAGS};${${cmake_built_type_config_name}}")

list(APPEND compile_options "${cmake_c_flags_sep}")

# Add the target triple to the compile flags if we are using clang
# It is not part of the CMAKE_C_FLAGS variable
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
    list(APPEND compile_options "${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
endif()

include(memoize)
# memoize this installation rule which will save the resulting artifact in a cache and reuse it across builds.
# This will rebuild from source if the git directory has changes or has a changed commit hash.
memoize_add_custom_command(musllibc "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage" "${CMAKE_CURRENT_SOURCE_DIR}" "${compile_options} ${muslc_arch}" ""
    OUTPUT
        build-temp/stage/lib/libc.a
        # If we have to rebuild, first clear the temporary build directory as
        # we have no correctly captured the output files or dependencies
    COMMAND rm -r build-temp
    COMMAND mkdir -p build-temp
    COMMAND
        ${CMAKE_COMMAND} -E env TOOLPREFIX=${CROSS_COMPILER_PREFIX}
        # TODO: should also pass in the cflags for the CMAKE_BUILD_TYPE ?
        # Suppress make output by default. Verbose make output can now be achieved
        # by MAKEFLAGS=V3 ninja (${MAKEFLAGS:--s} expands to -s if MAKEFLAGS is unset).
        C_COMPILER=${CMAKE_C_COMPILER}
        "MAKEFLAGS=\$\${MAKEFLAGS:--s}"
        NK_CFLAGS="${compile_options}" TARGET=${muslc_arch} make -C build-temp ${parallel} -f
        "${CMAKE_CURRENT_SOURCE_DIR}/Makefile"
        "STAGE_DIR=${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage"
        "SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
    DEPENDS ${deps} COMMAND_EXPAND_LISTS
    COMMENT "Invoking muslc build system"
)

add_custom_target(
    muslc_gen
    DEPENDS build-temp/stage/lib/libc.a
)

add_library(muslc_imported STATIC IMPORTED GLOBAL)
add_dependencies(muslc_imported muslc_gen)
set_property(
    TARGET muslc_imported
    PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/lib/libc.a"
)

add_library(muslc INTERFACE)
add_dependencies(muslc muslc_imported)
set_property(TARGET muslc PROPERTY INTERFACE_LINK_LIBRARIES muslc_imported)
target_include_directories(muslc INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/include")
